package com.gxuwz.Market.business.action.web;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.Market.business.entity.TemplateUpload;
import com.gxuwz.Market.business.entity.ServiceData;
import com.gxuwz.Market.business.entity.ServiceField;
import com.gxuwz.Market.business.entity.ServiceForm;
import com.gxuwz.Market.business.entity.ServiceTask;
import com.gxuwz.Market.business.entity.SysUser;
import com.gxuwz.Market.business.service.IServiceDataService;
import com.gxuwz.Market.business.service.IServiceFormService;
import com.gxuwz.Market.business.service.IServiceTaskService;
import com.gxuwz.Market.business.service.ServiceFormImpl;
import com.gxuwz.Market.business.service.ServiceUploadServiceImpl;
import com.gxuwz.core.pagination.Result;
import com.gxuwz.core.util.UidUtils;
import com.gxuwz.core.web.action.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import util.ExcelUtil;
import util.FieldUtil;

public class ServiceUploadAction extends BaseAction implements Preparable, ModelDriven<TemplateUpload> {

	private static final long serialVersionUID = 1L;
	protected static final String UPLOAD_File = "/WEB-INF/page/web/uploadFile.jsp";
	protected static final String FILE_LIST = "/WEB-INF/page/web/ServiceFile_list.jsp";
	protected static final String TPL_JSP = "/WEB-INF/page/web/ServiceForm_tpl.jsp";
	protected static final String EDIT_JSP = "/WEB-INF/page/web/ServiceForm_edit.jsp";
	protected static final String TASK_EDIT_FORM_JSP = "/WEB-INF/page/web/ServiceTask_edit_form.jsp";

	@Autowired
	private IServiceFormService serviceFormService;
	@Autowired
	private IServiceTaskService serviceTaskService;
	@Autowired
	private IServiceDataService serviceDataService;

	private File file;
	private String fileFileName;
	private String fileContentType;
	private Result pageResult;
	public TemplateUpload templateUpload;
	private List<ServiceField> fieldList;
	private ServiceForm serviceForm;
	private ServiceTask serviceTask;
	private String updata; // 是否导入数据
	private String mainId; // 用户主键

	@Autowired
	public ServiceUploadServiceImpl serviceUpload;

	@Override
	public void prepare() throws Exception {
		if (null == templateUpload) {
			templateUpload = new TemplateUpload();
		}
		if (serviceTask == null) {
			serviceTask = new ServiceTask();
		}

	}

	/**
	 * 打开文件上传的页面
	 * 
	 * @return
	 * @author 黄森南
	 * @throws IOException
	 * @date 2018.01.22
	 */
	public String openAdd() {

		forwardView = UPLOAD_File;
		return SUCCESS;

	}

	public String addFile() {
		List<ServiceData> dataList = new ArrayList<ServiceData>();
		// 读取格式
		serviceForm = ExcelUtil.excelToForm(file);

		// 获得数据
		if ("ture".equals(updata) && mainId != null) {
			// 生成格式表
			serviceForm.setFormId(UidUtils.UID());
			serviceForm.setCreateTime(new Timestamp(new Date().getTime()));
			serviceForm.setIsDelete(0);
			serviceFormService.add(serviceForm);
			serviceForm = serviceFormService.findByProperty("formId", serviceForm.getFormId()).get(0);

			// 获取主键下标值
			int index = ExcelUtil.getIndex(serviceForm, mainId);
			//读取数据
			dataList = ExcelUtil.excelToFormData(file, serviceForm.getFieldNum(), index);

			// 生成任务
			serviceTask.setTaskName(fileFileName);
			serviceTask.setStartTime(new Timestamp(new Date().getTime()));
			serviceTask.setTaskId(UidUtils.MINIUID());
			serviceTask.setIssuserId((String) getRequest().getSession().getAttribute("userId"));
			serviceTask.setFormId(serviceForm.getFormId());
			serviceTask.setTaskState(1);
			serviceTask.setTaskType(1);
			serviceTask.setDataName("service_" + serviceTask.getTaskId() + "_data");
			serviceTaskService.add(serviceTask);
			serviceForm.setFormState(1);
			serviceFormService.update(serviceForm);
			// 生成数据表
			serviceDataService.createDB(serviceTask.getTaskId(), serviceForm);
			// 导入数据
			serviceDataService.addDatas(serviceTask.getTaskId(), dataList, serviceForm.getFieldNum());
			setForwardView(TASK_EDIT_FORM_JSP);

		} else {
			setForwardView(TPL_JSP);
		}

		fieldList = FieldUtil.fieldOfForm(serviceForm);
		try {
			serviceUpload.fileUpload(file, fileFileName, fileContentType);
		} catch (IOException e) {

			new IOException("保存异常");
		}

		return SUCCESS;
	}

	/**
	 * 查询全部的文件列表（现在暂时不知道需求，就先查询全部，如果需要条件查询后续再改方法）
	 * 
	 * @return
	 * @author 黄森南
	 * @throws IOException
	 * @date 2018.01.22
	 */
	public String FileList() {
		/*
		 * SysUser sysUser= (SysUser)
		 * ActionContext.getContext().getSession().get("sysUser");
		 */
		try {
			pageResult = serviceUpload.findAll(getPage(), getRow());
		} catch (Exception e) {
			e.printStackTrace();
		}

		forwardView = FILE_LIST;
		return SUCCESS;
	}

	@Override
	public TemplateUpload getModel() {

		return null;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public TemplateUpload getTemplateUpload() {
		return templateUpload;
	}

	public void setTemplateUpload(TemplateUpload templateUpload) {
		this.templateUpload = templateUpload;
	}

	public Result getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result pageResult) {
		this.pageResult = pageResult;
	}

	public List<ServiceField> getFieldList() {
		return fieldList;
	}

	public void setFieldList(List<ServiceField> fieldList) {
		this.fieldList = fieldList;
	}

	public ServiceForm getServiceForm() {
		return serviceForm;
	}

	public void setServiceForm(ServiceForm serviceForm) {
		this.serviceForm = serviceForm;
	}

	public String getUpdata() {
		return updata;
	}

	public void setUpdata(String updata) {
		this.updata = updata;
	}

	public String getMainId() {
		return mainId;
	}

	public void setMainId(String mainId) {
		this.mainId = mainId;
	}

	public ServiceTask getServiceTask() {
		return serviceTask;
	}

	public void setServiceTask(ServiceTask serviceTask) {
		this.serviceTask = serviceTask;
	}

}
